<?php
class Kunx {

    public $defaultController = 'Index';
    public $defaultAction = 'Index';
    public static $controller;
    public static $action;

    public static function autoload($className) {
        if(isset(self::$coreClass[$className])) { // include core file
            $filename = FRAMEWORK_PATH . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . self::$coreClass[$className];
            if(file_exists($filename)) {
                include $filename;
            } else {
                throw new Exception($className . ' : '. $filename . ' NOT FOUND! PLEASE CHECK FILE NAME!','','','');
            }
        } else if(strpos($className, 'Controller')) { // include controller file
            $filename = CONTROLLER_PATH . DIRECTORY_SEPARATOR . $className . '.php';
            if(file_exists($filename)) {
                include $filename;
            } else {
                throw new Exception($className . ' : '.$filename . ' NOT FOUND! PLEASE CHECK FILE NAME!','','','');
            }
        } else if(strpos($className,'Model')) { // include model file
            $filename = MODEL_PATH . DIRECTORY_SEPARATOR . $className . '.php';
            if(file_exists($filename)) {
                include $filename;
            } else {
                throw new Exception($className . ' : '.$filename . ' NOT FOUND! PLEASE CHECK FILE NAME!');
            }
        } else { // include view file
            $filename = VIEW_PATH . DIRECTORY_SEPARATOR . $className . '.php';
            if(file_exists($filename)) {
                include $filename;
            } else {
                throw new Exception($className . ' : '.$filename . ' NOT FOUND! PLEASE CHECK FILE NAME!');
            }
        }
    }

    public static function run($config) {
        self::setRoute($config['path_type']);
        $controller = ucfirst(self::$controller) . 'Controller';
        $action = 'action' . ucfirst(self::$action);
        $controller = new $controller(ucfirst(self::$controller), ucfirst(self::$action));
        $controller->$action();
    }

    //设置路由,设置控制器和动作
    public static function setRoute($pathType) {
        if ($pathType == 'path') {
            if (isset($_SERVER['PATH_INFO'])) {
                $routes = explode('/', $_SERVER['PATH_INFO']);
                self::$this->controller = array_shift($routes);
                self::$action = array_shift($routes);
            }
        } else if ($pathType == 'arg') {
            $routes = explode('/', $_GET['r']);
            self :: $controller = array_shift($routes);
            self::$action = array_shift($routes);
        }
    }

    private static $coreClass = array(
        'KController' => 'KController.php',
        'KModel'=> 'KModel.php',
    );
}

class KException extends Exception {
    public static function printException($e) {
        echo 'Exception:' . $e->getMessage() . '<br />';
    }
}

function printError($errorNo,$errorStr,$errorFile,$errorLine) {
    if(strpos($errorStr, 'Undefined variable:') === 0) {
        $errorStr = str_replace('Undefined variable: ', '变量', $errorStr) . '未定义!';
    }
    if (strpos($errorStr, 'Undefined property:') === 0) {
        $errorStr = str_replace('Undefined property: ', '成员属性', $errorStr) . '未定义!';
    }
    $errorStr = str_replace('failed to open stream:', '无法打开数据流,', $errorStr);
    $errorStr = str_replace('No such file or directory', '文件未找到!', $errorStr);
    echo $errorFile . ' 第 ' . $errorLine . ' 行 ' . ' 错误 ' . $errorStr . '<br />';
}

spl_autoload_register(array('Kunx','autoload')); // registe auto include file function
set_exception_handler(array('KException','printException')); // registe exception function
set_error_handler('printError',E_ALL);
        
?>